from flask import (
    Blueprint, json
)
from blemesh import db
from .mesh import login_request,r
from .error import error_handle

grant = db.Grant()

bp = Blueprint('grant', __name__, url_prefix='/grant')

@bp.route('/meshs_granted_to_me/', methods=['GET'])
@error_handle
@login_request
def meshs_granted_to_me(user_id, res):
    print("____________________________________")
    B_id = user_id
    res['data'] = grant.meshs_granted_to_me(B_id)
    res['code'] = 0
    return json.dumps(res)

@bp.route('/get_granted_list/', methods=['GET'])
@error_handle
@login_request
def get_granted_list(user_id, res):
    res['data'] = grant.get_member_granted(user_id)
    res['code'] = 0
    return json.dumps(res)

@bp.route('/grantee_and_meshs/', methods=['POST'])
@error_handle
@login_request
def grantee_and_meshs(user_id, req, res):
    A_id= user_id
    B_id = req['B_id']
    res['granted'] = grant.get_my_meshs_granted_to_someone(A_id,B_id)
    res['ungranted'] = grant.get_my_meshs_ungranted_to_someone(A_id, B_id)
    res['code'] = 0
    return json.dumps(res)

@bp.route('/add_grant_member/', methods=['POST'])
@error_handle
@login_request
def add_grant_member(user_id, req, res):
    A_id= user_id
    session_id = req['B_id']
    print("session_________id",session_id)
    B_id = r.get(session_id)
    print("user_________id", B_id)
    nickname =  grant.get_granted_name(B_id)
    res['data'] = grant.add_grant_member(A_id, B_id, nickname)
    res['code'] = 0
    return json.dumps(res)


@bp.route('/rename_granted_member/', methods=['POST'])
@error_handle
@login_request
def rename_granted_member(user_id, req, res):
    A_id= user_id
    print("req in rename_granted_member",req)
    B_id = req['B_id']
    nickname =  req['nickname']
    grant.rename_granted_member(A_id,B_id,nickname)
    res['code'] = 0
    return json.dumps(res)

@bp.route('/delete_member/', methods=['POST'])
@error_handle
@login_request
def delete_member(user_id, req, res):
    A_id= user_id
    B_id = req['B_id']
    grant.delete_member(A_id,B_id)
    res['code'] = 0
    return json.dumps(res)

@bp.route('/add_mesh_to_member/', methods=['POST'])
@error_handle
@login_request
def add_mesh_to_member(user_id, req, res):
    A_id= user_id
    B_id = req['B_id']
    mesh_id = req['mesh_id']
    grant.add_mesh_to_member(A_id,B_id,mesh_id)
    res['code'] = 0
    return json.dumps(res)

@bp.route('/add_mesh_to_member_first/', methods=['POST'])
@error_handle
@login_request
def add_mesh_to_member_first(user_id, req, res):
    A_id= user_id
    B_id = req['B_id']
    B_id = r.get(B_id)
    mesh_id_list = req['mesh_id_list']
    for mesh_id in mesh_id_list:
        grant.add_mesh_to_member(A_id,B_id,mesh_id)
    res['code'] = 0
    return json.dumps(res)

@bp.route('/delete_mesh_of_member/', methods=['POST'])
@error_handle
@login_request
def delete_mesh_of_member(user_id, req, res):
    A_id= user_id
    B_id = req['B_id']
    mesh_id = req['mesh_id']
    grant.delete_grant_mesh(A_id,B_id,mesh_id)
    res['code'] = 0
    return json.dumps(res)



